﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Maps.Toolkit;
using System.Windows.Media;
using System.Threading;
using Windows.Devices.Geolocation;
using RadarGlobalPro.MapClass;

namespace RadarGlobalPro
{
    class MapDraw
    {
        private Map MapControl{set;get;}

        MapLayer LayerDraw = new MapLayer();

        CustomPushpin MePushpin = new CustomPushpin();

        MapPolygon radarRange;

        MapPolyline LineScan;

        int Angle = 0;

        public MapDraw(Map MapControl)
        {
            this.MapControl = MapControl;

            InitializeRadarRanger();

            InitializeMePushpin();           

            MapControl.MapElements.Add(radarRange);

            //MapControl.MapElements.Add(radarRange2);

            MapControl.MapElements.Add(LineScan);

            InitializeLineScan();
        }
        public MapPolygon getRadar()
        {
            return radarRange;
        }
        private void InitializeRadarRanger()
        {
            var fill = Colors.Green;

            var stroke = Colors.Black;

            fill.A = 80;

            stroke.A = 80;

            radarRange = new MapPolygon
            {
                StrokeThickness = 10,

                FillColor = fill,

                StrokeColor = stroke,

                StrokeDashed = false,
            };

            LineScan = new MapPolyline();

            LineScan.StrokeColor = Colors.Green;

            LineScan.StrokeThickness = 5;        
    
            //radarRange2 =new MapPolygon
            //{
            //    StrokeThickness = 10,

            //    FillColor = Color.FromArgb(0,0,0,0),

            //    StrokeColor = stroke,

            //    StrokeDashed = false,
            //};  
        }

        private void InitializeMePushpin()
        {
            //MePushpin.SetCircler2(MapControl, Colors.Green, Colors.LightGray);
            MePushpin.SetCircler(MapControl,20,20);
            //MePushpin.SetImagePushpin(MapControl);
        }

        private void InitializeLineScan()
        {
        }
        public void TransForms(GeoCoordinate center,double Radius=1000)
        {
            Angle++;
            IList<GeoCoordinate> ListPoint = center.GetCirclePoints(Radius);
            if (Angle >= ListPoint.Count) Angle = 0;
            LineScan.Path.Clear();
            LineScan.Path.Add(center);
            LineScan.Path.Add(ListPoint[Angle]);
        }

        public void UpdateMyLocation(GeoCoordinate center)
        {
            MePushpin.UpdateLocation(center);
        }

        public void UpdateRadarLocation(GeoCoordinate Center, double Radius = 1000)
        {
            UpdateMyLocation(Center);

            if (radarRange == null) InitializeRadarRanger();

            radarRange.Path.Clear();

            foreach (var p in Center.GetCirclePoints(Radius))
            {

                radarRange.Path.Add(p);

            }
            //radarRange2.Path.Clear();
            //foreach(var p in Center.GetCirclePoints(Radius/2))
            //{
            //    radarRange2.Path.Add(p);
            //}  
        }
    }
}
